<template>
  <div class="comment">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.id">
        <template #title>
          <div class="cmt-wrap">
            <van-image round fit="cover" :src="item.aut_photo"></van-image>
            <div class="info">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">
                {{ item.pubdate | reltTime }}
                <van-button @click="showReply(item)" size="mini"
                  >回复({{ item.reply_count }})</van-button
                >
              </div>
            </div>
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>

    <!-- 输入的区域 -->
    <div class="cmt-input-wrap">
      <van-field
        v-model="cmtValue"
        @keyup.enter="send"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>
    <van-action-sheet v-model="sheetShow" title="发表回复">
      <reply v-if="sheetShow" :currentItem="currentItem" />
    </van-action-sheet>
  </div>
</template>
<script>
import { commentAPI } from "@/api";
import { sendCmtAPI } from "@/api";
import reply from "./reply";
export default {
  components: {
    reply,
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      cmtValue: "",
      sheetShow:false,
      currentItem:{},

    };
  },
  methods: {
    async onLoad() {
      let res = await commentAPI({
        type: "a",
        source: this.$route.query.id,
        offset: this.offset,
      });
      //  console.log(res);
      this.list.push(...res.data.data.results);
      this.offset = res.data.data.last_id;
      this.loading = false;
      if (res.data.data.last_id == res.data.data.end_id) {
        this.finished = true;
      }
    },
    async send() {
      if (this.cmtValue == "") {
        this.$toast("请输入内容");
        return;
      }
      let res = await sendCmtAPI({
        //文章id
        target: this.$route.query.id,
        content: this.cmtValue,
      });
      // console.log(res);
      this.list.unshift(res.data.data.new_obj);
      this.cmtValue = "";
      this.$toast("发布评论成功");
    },
      showReply (item) {

      // 1. 显示出回复面板
      this.sheetShow = true
      // 2. 把当前被点的评论传递给回复面板
      this.currentItem = item
    },
  },
};
</script>
<style lang="less" scoped>
.comment {
  margin-top: 20px;

  ::v-deep{

   .cmt-wrap {
    display: flex;
    .van-image {
      width: 45px;
      height: 45px;
    }
    .info {
      margin-left: 20px;
      flex: 1;
      .name {
        color: #466b9d;
      }

      .content {
        margin: 8px 0;
      }

      .time {
        height: 20px;
        display: flex;
        align-items: center;
        color: gray;
      }
    }
  }
 }
  .cmt-input-wrap {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }
}
</style>
